Attribute VB_Name = "modStackFree"
'***************************************************************
' (c) Copyright 2000 Matthew J. Curland
'
' This file is from the CD-ROM accompanying the book:
' Advanced Visual Basic 6: Power Techniques for Everyday Programs
'   Author: Matthew Curland
'   Published by: Addison-Wesley, July 2000
'   ISBN: 0-201-70712-8
'   http://www.PowerVB.com
'
' You are entitled to license free distribution of any application
'   that uses this file if you own a copy of the book, or if you
'   have obtained the file from a source approved by the author. You
'   may redistribute this file only with express written permission
'   of the author.
'
' This file depends on:
'   References:
'     VBoostTypes6.olb (VBoost Object Types (6.0))
'   Files:
'     FunctionDelegator.bas
'   Minimal VBoost conditionals:
'     None
'   Conditional Compilation Values:
'     FUNCTIONDELEGATOR_NOHEAP = 1 'Minimize FunctionDelegator impact
'
' This file is discussed in Chapter 11.
'***************************************************************
Option Explicit
'// Pop the stack by the given number of bytes
'mov ecx, [esp + 4] ;get the number of bytes to pop
'mov eax, [esp]     ;get the return address
'add ecx, 4         ;adjust for incoming parameter
'add esp, ecx       ;shift the return address down by the correct number of bytes
'mov [esp], eax     ;move the return address into the correct position on the stack
'ret                ;get out of here

Private Type StackFreeCode
    Code(3) As Long '4 Longs for popping stack
End Type
Private Code As StackFreeCode
Private FD As FunctionDelegator
Private pCallObj As ICallLongReturnVoid
Public Property Get StackFree() As ICallLongReturnVoid
    If pCallObj Is Nothing Then Init
    Set StackFree = pCallObj
End Property

'This is frequently used with StackAlloc. It is included
'here instead of with StackAlloc or StackAllocZero because
'this file is used along with both of the alloc files.
Public Function AlignedSize(ByVal Size As Long) As Long
Const PTRSIZE As Long = 4
    AlignedSize = Size + _
      (PTRSIZE - ((Size - 1) And (PTRSIZE - 1))) - 1
End Function
Private Sub Init()
    With Code
        .Code(0) = &H4244C8B
        .Code(1) = &H8324048B
        .Code(2) = &HE10304C1
        .Code(3) = &HC3240489
        Set pCallObj = InitDelegator(FD, VarPtr(.Code(0)))
    End With
End Sub



